<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/form_css.css" rel="stylesheet" type="text/css"  />
<!-- begin ckeidtor -->
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({			 
	theme : "advanced",
	mode : "textareas",
    plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,fullscreen,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	theme_advanced_font_sizes : "10px,12px,14px,16px,24px",
});
</script>
<!-- end cheditor-->
</head>
<?php
	//select value from DB to show on textbox
	$pro_id = $_REQUEST["pro_id"];
	if(isset($_REQUEST["pro_id"]))
	{
		
		
		$sql = "SELECT * FROM tbl_product WHERE pro_id={$pro_id} LIMIT 1";
		$result = query($sql);
		$row = fetch_assoc($result);
		
		$categoryId=$row["cat_id"];
		$product=$row["pro_name"];
		$description=$row["pro_description"];
		$price=$row["price"];
		$statusId=$row["sta_id"];
		$showFront=$row["show_front"];
		$publish=$row["published"];
		$uploadfile = $row["photo"];
	}
?>
<?php
                    if(isset($_POST["btnsubmit"]))
                    {
						$fieldimage = "";
						$oldfilename = $_POST["txtoldimage"];
						$newfilename = escapeValue($_FILES["uploadfile"]["name"]);

						$pro_id = $_POST["txtpro_id"];
							
						$categoryId=$_POST["cbocategory"];
						$product=escapeValue($_POST["txtproduct"]);
						$description=escapeValue($_POST["txtdescription"]);
						$price=escapeValue($_POST["txtprice"]);
						$statusId=$_POST["cbostatus"];
						$showFront=$_POST["cboshowFront"];
						$publish=$_POST["cbopublish"];
						
						if($newfilename=="")
						{
							if(@$errors == "")
										{
											$sql="UPDATE tbl_product SET pro_name='$product',
											      cat_id='$categoryId', pro_description='$description',
												  price='$price', sta_id='$statusId', show_front='$showFront',
												  published='$publish' WHERE pro_id={$pro_id}
											";													
											if(query($sql))
											{
													errorMessage("Mise à jour réussie ...!");
													
													//direct to itself and get pro_id=ocure pro_id
													//pageRedirect("productEdit.php?pro_id=$pro_id","");
											}
										}
						}//  $newfilename =""
						else
						{
							if(@$errors == "")
								{
									if($categoryId=="")
									{
										errorMessage("S'il vous plaît sélectionner la catégorie ...!");	
									}
									elseif($product=="")
									{
										errorMessage("S'il vous plaît d'entrée du produit ...!");	
									}
									
									else
									{
										//Upload Image

										$image_name = $_FILES["uploadfile"]["name"];
										$image_tmp_name = $_FILES["uploadfile"]["tmp_name"];
										$ext = "jpg";	
										$tag = "";
										$path = "product";
										
										
										
										// Front Photo		
										//$filename = "$image_name-f.$ext";	
										$width = 146;
										$height = 95;			
										createImageAndThumb($image_tmp_name,$image_name,
															$width,
															$height,
															$tag,
															$path,
															$image_name);
										// detail Photo
										$filename_d = "$uploadfile-d.$ext";	
										$width = 184;
										$height = 211;			
										createImageAndThumb($image_tmp_name,$filename_d,
															$width,
															$height,
															$tag,
															$path,
															$image_name);
										
										//end upload image
										
										if(@$errors == "")
										{
											$sql="UPDATE tbl_product SET pro_name='$product',
											      cat_id='$categoryId', pro_description='$description',
												  price='$price', sta_id='$statusId', show_front='$showFront',
												  published='$publish', photo='$newfilename' WHERE pro_id={$pro_id}
											";													
											if(query($sql))
											{
													//delete old image
													$old_d = "$oldfilename-d.$ext";	
													$old_r = "$oldfilename-r.$ext";
														
													deleteFile("product/{$oldfilename}");
													deleteFile("product/{$old_d}");
													deleteFile("product/{$old_r}");
													
													errorMessage("Mise à jour réussie ...!");
													
													//direct to itself and get pro_id=ocure pro_id
													//pageRedirect("productEdit.php?pro_id=$pro_id","");
											}
										}
										
									}//end else			
													
								}//end check error=""
								
						}//end else $newfilename != ""
		
                    } //end isset btnsubmit
                ?>
                		<div id="border_cont_title">
                        	<div id="content_title_left">Modifier produit</div>
                            <div id="content_title_right"></div>
                        </div>
                        
					<?php echo @$errors;?>
                    	 <!--start table -->
                    <form action="index.php?page=productEdit&pro_id=<?php echo $pro_id;?>" method="post" enctype="multipart/form-data">
                    	<table width="765px" align="left">
                        	<tr>
                            	
								<input type="hidden" value="<?php echo $pro_id; ?>" name="txtpro_id" />

                            	<td align="left" height="30" bgcolor="#FFEAD7">Catégorie*</td>
                                <td>
                                	<select name="cbocategory">
                        				<option value=""> -- Choisir une catégorie -- </option>
								<?php
                                    $sql = "SELECT * FROM tbl_category WHERE public=1";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$categoryId == $row['cat_id'])
                                            $selected = " selected='selected'";
                                        else	
                                            $selected = "";
                                ?>
                                        <option value="<?php echo $row['cat_id'];?>"<?php echo $selected;?>>
                                        <?php echo $row['cat_name'];?></option>
                                <?php
                                    }
                                    
                                ?>
                       			 </select>
                                </td>
                                
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Produit*</td>
                                <td>
                                <input type="text" name="txtproduct" value="<?php echo @$product; ?>" size="41"/>
                                </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Description</td>
                                 <td>
                                <textarea name="txtdescription" class="ckeditor" cols="40" rows="10" id="txtdescription" wrap="virtual" style="font-family:Arial, Helvetica, sans-serif; font-size:12px"><?php echo @$description;?></textarea>
                                 </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Prix</td>
                                 <td>
                                <input type="text" name="txtprice" value="<?php echo @$price;?>" />
                                 </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Statut</td>
                                <td align="left" height="30">
                                	<select name="cbostatus">
                        	<option value=""> -- Sélectionnez Etat -- </option>
                            	
								<?php
                                    $sql = "SELECT * FROM tbl_status";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$statusId == $row['sta_id'])
                                            $selected = " selected='selected'";
                                        else	
                                            $selected = "";
                                ?>
                                        <option value="<?php echo $row['sta_id'];?>"<?php echo $selected;?>>
                                        <?php echo $row['sta_name'];?></option>
                                <?php
                                    }
                                    
                                ?>
                        </select>
                                </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Voir avant</td>
                                <td align="left" height="30">
                                	<select name="cboshowFront" >
                                       <option value=""> -- Select Show Front -- </option>
									<?php
										$sql = "SELECT * FROM tbl_zeroone";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$showFront == $row['zeroone_value'])
                                            $selected = " selected='selected'";
											
                                        else	
                                            $selected = "";
                                	?>
                                        <option value="<?php echo $row['zeroone_value'];?>"<?php echo $selected;?>>
                                        <?php echo $row['zeroone_name'];?></option>
                                        
                                	<?php
										
                                    }
                                   
                                  	?>
                                        
                                    </select>
                                </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Publier</td>
                                <td align="left" height="30">
                                	<select name="cbopublish" >
                                    	<option value=""> -- Select Publish -- </option>
                                    	<?php
										$sql = "SELECT * FROM tbl_zeroone";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$showFront == $row['zeroone_value'])
                                            $selected = " selected='selected'";
											
                                        else	
                                            $selected = "";
                                	?>
                                        <option value="<?php echo $row['zeroone_value'];?>"<?php echo $selected;?>>
                                        <?php echo $row['zeroone_name'];?></option>
                                        
                                	<?php
										
                                    }
                                   	?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Photo</td>
                                 <td>
                                <input class="submit" type="file" name="uploadfile" /><img src="product/<?php echo @$uploadfile ;?>" width="80px" height="70px" />
                                <input type="hidden" value="<?php echo @$uploadfile; ?>" name="txtoldimage" />
                                 </td>
                            </tr>
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Action</td>
                                <td align="left" height="30">
                                <input class="submit" type="submit" name="btnsubmit" value="Soumettre"  />
                                <input class="submit" type="button" name="btncancel" value="Annuler" onclick="redirect('index.php?page=product')" />
                                </td>
                            </tr>
                            
                        </table>
                        </form>
                    <!--end table -->					
</body>
</html>
